コンソールで動くテトリスを実装するチュートリアル その1 文字だけで画面を描画する
この作業用リポジトリは以下
作業スペースの作成
まず作業用のリポジトリを作る
code:sh
mkdir console_tetris_tutorial
cd console_tetris_tutorial
git init
nimble init
nimbleファイルを以下のように変更
code:console_tetris_tutorial.nimble
# Package
version = "0.1.0"
author = "jiro4989"
description = "A new awesome nimble package"
license = "MIT"
srcDir = "src"
binDir = "bin"
# Dependencies
requires "nim >= 1.2.2"
requires "illwill >= 0.1.0"
合わせてソースファイル名もリネームする
code:sh
mv src/console_tetris_tutorial.nim src/tetris1.nim
ビルドが通ることを確認する
code:sh
nimble build
ls ./bin/
画面描画処理の実装
ボードを2次元のシーケンスで表現
0 が空のセル
1 が壁のセル
わかりやすさのためにfromで明示的に使うプロシージャを指定している
code:tetris1.nim
from os import sleep
from strutils import join
from sequtils import mapIt
type
const
initialBoard*: Board = @[
@1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, @1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, @1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ]
while true:
for row in initialBoard:
# シーケンスの各要素を文字列に変換してから結合
echo row.mapIt($it).join
echo "---"
sleep 1000 # ミリ秒
これで動作確認する
code:sh
nimble build
./bin/tetris1
結果が以下
https://gyazo.com/ea57634469d1bb7d0c97cd0c43cefe48
同じボードが無限に表示され続ける